package cn.hzc.fortess.system.controller;


import cn.hzc.fortess.core.common.BizErrorEnum;
import cn.hzc.fortess.core.common.ResponseData;
import cn.hzc.fortess.core.common.annotion.CheckType;
import cn.hzc.fortess.core.common.annotion.RequestPermission;
import cn.hzc.fortess.core.common.constant.Const;
import cn.hzc.fortess.core.common.exception.ServiceException;
import cn.hzc.fortess.core.shiro.ShiroKit;
import cn.hzc.fortess.core.shiro.ShiroUser;
import cn.hzc.fortess.system.entity.Project;
import cn.hzc.fortess.system.page.Pagination;
import cn.hzc.fortess.system.search.ProjectSearch;
import cn.hzc.fortess.system.service.ProjectService;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Objects;

@Api(tags = {"项目管理"})
@Controller
@RequestMapping("/sam/project")
public class ProjectController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(ProjectController.class);


    @Autowired
    private ProjectService projectService;


    @ApiIgnore
    @RequestMapping(value = "",method = RequestMethod.GET)
    public String index() {
        return buildToView("index.html");
    }


    @ApiIgnore
    @RequestMapping("/add")
    public String addToView() {
        setAttr("project", new Project());
        return buildToView("form.html");
    }

    @ApiOperation("创建项目")
    @RequestMapping(value = "/add.json", method = RequestMethod.POST)
    @RequestPermission(value = Const.ADMIN_ROLE_NAME, checkType = CheckType.ROLE) //只有管理员才有权限
    @ResponseBody
    public ResponseData create(Project entity) {
        this.projectService.add(entity);
        return ResponseData.success();
    }

    @ApiIgnore
    @RequestMapping("/edit/{id}")
    public String editToView(@PathVariable("id") Integer id, Model model) {
        setAttr("project", projectService.getById(id));
        return buildToView("form.html");
    }

    @ApiOperation("编辑项目")
    @RequestMapping(value = "/edit.json", method = RequestMethod.POST)
    @ResponseBody
    @RequestPermission("sam:project:edit")
    public ResponseData edit(Project entity) {
        this.projectService.update(entity);
        return ResponseData.success();
    }

    @ApiOperation("删除项目")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    @RequestPermission(value = Const.ADMIN_ROLE_NAME, checkType = CheckType.ROLE) //只有管理员才有权限
    public ResponseData delete(@PathVariable(value = "id", required = true) Integer id) {
        if (Objects.equals(id, Const.SAM_PROJECT_ID)) {
            throw new ServiceException(BizErrorEnum.CANT_DELETE_SAM);
        }
        log.info("del project ..id {}", id);
        this.projectService.delete(id);
        return ResponseData.success();
    }

    @ApiOperation("分页列表")
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @RequestPermission("sam:project:list")
    public Pagination page(ProjectSearch search) {

        return this.projectService.getPage(search);
    }

    @ApiIgnore
    @RequestMapping(value = "/userproject", method = RequestMethod.GET)
    public String userProjectList(Model model) {
        ShiroUser user = ShiroKit.getUserNotNull();

        List<Project> projects = null;
        if (user.isAdministrator()) {
            projects = projectService.getUserProject();
        } else {
            projects = projectService.getProjectByUserRole(user);
        }
        //只有一个项目时，页面不显示多项目
        if(CollectionUtils.isEmpty(projects)|| projects.size()==1){
            model.addAttribute("projects", Lists.newArrayList());
        }else{
            model.addAttribute("projects", projects);
        }
        model.addAttribute("projectId", getSession().getAttribute(KEY_SESSION_CURRENT_PROJECT_ID));

        return "/system/oms.html";
    }


    @Override
    protected String moduleName() {
        return "project";
    }
}


